<?php
/**
 * Created by PhpStorm.
 * User: niqiaoyun
 * Date: 2017/9/5
 * Time: 09:28
 */

namespace app\models;


use app\models\dao\OperationLog;

class PermissionModel extends OperationLog {
	public function getAll($query = [], $page = []) {
		$where = [];
		if (!empty($query['where'])) {
			$where = $query['where'];
		}

		$query_order = ['create_time' => SORT_ASC];
		$query_wheres = ['and'];
		$query_wheres[] = " 1 ";
		if (!empty($where['action'])) {
			$query_wheres[] = ['action' => $where['action']];
		}
		// 读取总条数
		$count = $this->count($query_wheres);

		// 计算分页
		$page = Tools::getPage($count, $page);
		$arr_rows = OperationLog::find()->with('user')->where($query_wheres)->orderBy($query_order)->limit($page['limit'])->offset($page['offset'])->asArray()->all();
//		echo "<pre>";print_r($arr_rows);exit;
		return Tools::formatPage($arr_rows, $count);
	}

	public function getOne($int_id) {
		$arr_rows = OperationLog::find()->where(['id' => $int_id, 'flag' => 1])->one();
		if (empty($arr_rows)) {
			return array();
		}
		return $arr_rows;
	}

	public function create($array_params) {
		$createObj = new OperationLog();

		$bool_save = $createObj->setData($array_params)->insert();
		if (true === $bool_save) {
			return array('status' => true, 'msg' => 'success', 'data' => array('create_time' => $array_params['create_time']));
		} else {
			return array('status' => false, 'msg' => 'error');
		}
	}

	/**
	 * 更新行业分类信息
	 * @param array $data
	 */
	public function modify($data = []) {
		// 保存数据
		$int_id = (int) $data['id'];
		unset($data['id']);
		$modifyObj = new OperationLog();
		$update_result = $modifyObj->updateAll($data, 'id = :id', [':id' => $int_id]);
		if ($update_result >= 0) {
			return true;
		} else {
			return false;
		}
	}

	public function count($where = []) {
		$count = OperationLog::find()->where($where)->asArray()->count(1);
		return $count;
	}
}